package com.anivia.common.web.config.mybatisPlus;

import com.anivia.common.web.satoken.util.UserInfoUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 字段填充
 *
 * @author wangj
 */
@Component
public class MetaHandler implements MetaObjectHandler {

    /**
     * 新增数据执行
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("version", 0, metaObject);
        this.setFieldValByName("deleted", 0, metaObject);
        this.setFieldValByName("creatorId", UserInfoUtil.getLoginUserId(), metaObject);
    }

    /**
     * 更新数据执行
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updaterId", UserInfoUtil.getLoginUserId(), metaObject);
    }
}